﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using miniword.log;

namespace miniword.compiler
{
    class Compiler
    {
        private const string TAG = "Compiler";

        private static string idlCompilerPath = System.Windows.Forms.Application.StartupPath + @"\mico\";
        private const string IDL_EXE = "idl.exe";
        private const string IDL_DEFAULT_ARG = "--c++-impl --c++-skel";

        private static string asnCompilerPath = System.Windows.Forms.Application.StartupPath + @"\asn1c\";
        private const string ASN_EXE = "asn1c.exe";
        private const string ASN_DEFAULT_ARG = "-fnative-types";

        public static void idlCompiler(string fileName, string arg)
        {
            callEXE(IDL_EXE, idlCompilerPath, arg + " " + fileName);
        }

        public static void idlCompiler(string fileName)
        {
            idlCompiler(fileName, IDL_DEFAULT_ARG);
        }

        public static void asn1cCompiler(string fileName, string arg)
        {
            callEXE(ASN_EXE, asnCompilerPath, arg + " " + fileName);
        }

        public static void asn1cCompiler(string fileName)
        {
            asn1cCompiler(fileName, ASN_DEFAULT_ARG);
        }

        private static void callEXE(string fileName, string wDirect, string arg)
        {
            Log.d(TAG,"compiler arg is : "+arg);
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = fileName;
            p.StartInfo.Arguments = arg;
            p.StartInfo.WorkingDirectory = wDirect;
            p.Start();
            if (p.HasExited)
            {
                p.Kill();
            }
            else
            {
                //do nothing
            }
        }
    }
}
